package com.sand.controller;

import com.alibaba.fastjson.JSON;
import com.sand.entity.QuestiondetailsEntity;
import com.sand.entity.TRoleEntity;
import com.sand.entity.TUserEntity;
import com.sand.service.IQuestionService;
import com.sand.service.IUserService;
import com.sand.util.ModelTest;
import com.sand.util.PageModel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "question")
public class QuestionController {
/**
 *
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/12/2/002 17:26
 */
    //userService接口
    @Resource(name = "questionService")
    private IQuestionService questionService;

    //分页查询提问
    @RequestMapping(value = "/queryQuestionLimit")
    public void queryQuestionLimit(int limit, int page, String condition,int showWi, HttpServletRequest request, HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");
        PageModel<HashMap<String, Object>> pm = questionService.queryQuestionLimit(limit, page, condition);
        switch (showWi){
            case 1:
                ModelTest<List<HashMap<String, Object>>> listData = new ModelTest<List<HashMap<String, Object>>>("OK",pm.getList(),0, pm.getPagecount());
                response.getWriter().write(JSON.toJSON(listData).toString());
                break;
            case 2:
                response.getWriter().write(JSON.toJSON(pm.getList()).toString());
                break;
        }

    }
    //根据id查询提问
    @RequestMapping(value = "/queryQuestionById")
    public ModelAndView queryQuestionById(int rid,int showWi){
        ModelAndView mv = new ModelAndView();
        Map<String, Object> map = questionService.queryQuestionById(rid);
        mv.addObject("obj",map.get("obj"));
        mv.addObject("list",map.get("list"));

        for(Map.Entry<String, Object> entry : map.entrySet()) {
            System.out.println("Key:"+entry.getKey()+" value:"+entry.getValue());
        }

        switch (showWi){
            case 1://后台页面显示
                mv.setViewName("layui/page/table_question_show");
                break;
            case 2://前台页面显示
                mv.setViewName("");
                break;
        }
        return mv;
    }
    //根据小区id查询所有提问
    @RequestMapping(value = "/queryQuestionByCommunity")
    public void queryQuestionByCommunity(int rid,HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");
        List<HashMap<String, Object>> list = questionService.queryQuestionByCommunity(rid);
        response.getWriter().write(JSON.toJSON(list).toString());
    }
    //删除提问
    @RequestMapping(value = "/delQuestion")
    public void delQuestion(String data,HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");

        boolean bln = questionService.delQuestion(data);
        if(bln){
            response.getWriter().write("OK");
        }else {
            response.getWriter().write("NO");
        }
    }
    //添加提问
    @RequestMapping(value = "/addQuestion")
    public void addQuestion(String data,HttpServletResponse response)throws Exception{

        response.setCharacterEncoding("utf-8");
        boolean bln = questionService.addQuestion(data);
        if(bln){
            response.getWriter().write("OK");
        }else {
            response.getWriter().write("NO");
        }
    }
    //删除回答
    @RequestMapping(value = "/delQuestionDetails")
    public void delQuestionDetails(String data,HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");

        boolean bln = questionService.delQuestionDetails(data);
        if(bln){
            response.getWriter().write("OK");
        }else {
            response.getWriter().write("NO");
        }
    }
    //添加回答
    @RequestMapping(value = "/addQuestionDetails")
    public void addQuestionDetails(String data,HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");
        boolean bln = questionService.addQuestionDetails(data);
        if(bln){
            response.getWriter().write("OK");
        }else {
            response.getWriter().write("NO");
        }
    }

    @RequestMapping("Ask")
    //跳转回答
    public String ask(Model model,int rid ,String content,String communityName,String time){
      List<QuestiondetailsEntity>   qu = questionService.SelectQuestionId(rid);
      for (QuestiondetailsEntity u : qu){
          System.out.println(u);
      }
        model.addAttribute("content",content);
        model.addAttribute("tim",time);
        model.addAttribute("rid",rid);
        model.addAttribute("communityName",communityName);
        model.addAttribute("Question",qu);
        return "renting/pages/onther/ask";
    }
}
